package panel.selfPathList
{
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.setTimeout;
	
	import inerface.ISelfPathEditPanel;
	
	import main.ClientConstants;
	
	import panel.eating.DragContainerBox;
	
	import struct.SelfPath;
	
	public class SelfPathEditPanel extends Sprite implements ISelfPathEditPanel
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		
		private var saveChangeBtn:Sprite;
		private var titleSprite:Sprite;
		private var inputFormat:TextFormat;
		
		private var dragContainer:DragContainerBox;
		private var boxList:SelfPathEditContainer;
		
		private var _onSave:Function;

		private var titleText:TextField;
		
		private var pathObj:SelfPath;
		
		public function SelfPathEditPanel()
		{
		}
		
		public function changeData(obj:SelfPath):void
		{
			pathObj = obj;
			
			titleText.text = pathObj.name;
			titleText.setTextFormat(inputFormat);
			
			boxList.pathData = pathObj;
			
			boxList.changeData(obj.info);
			
			dragContainer.changeData([boxList]);
		}
		
		public function set saveChange(func:Function):void
		{
			_onSave = func;
		}
		
		public function init(stage:Stage, loader:ResourceLoader, lang:Lang, uri:URI=null):void
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			initThis();
		}
		
		private function initThis():void
		{
			var img1:Bitmap = new _loader.uiManager.componentBack as Bitmap;
			img1.width = _stage.stageWidth * .5;
			img1.height = _stage.stageHeight * .1;
			
			titleText = new TextField;
			titleText.wordWrap = true;
			titleText.multiline = true;
			titleText.selectable = true;
			titleText.autoSize = TextFieldAutoSize.CENTER;
			titleText.type = TextFieldType.INPUT;
			
			titleText.width = _stage.stageWidth * .4;
			titleText.height = _stage.stageHeight * .05;
			
			inputFormat = new TextFormat;
			inputFormat.bold = true;
			inputFormat.color = 0x000000;
			inputFormat.size = HtmlText.FONT_SIZE + 2;
			
			titleSprite = new Sprite;
			this.addChild(titleSprite);
			
			titleSprite.addChild(img1);
			titleSprite.x =0;
			titleSprite.y = _stage.stageHeight * ClientConstants.HOME_UP_PER;
			
			titleSprite.addChild(titleText);
			titleText.y = _stage.stageHeight * .03;
			titleText.x = _stage.stageWidth * .05;
			
			titleText.addEventListener(TextEvent.TEXT_INPUT,changeStyleHandler);
			
			//================================================
			
			var img:Bitmap = new _loader.uiManager.componentBack as Bitmap;
			img.width = _stage.stageWidth * .5;
			img.height = _stage.stageHeight * .1;
			
			var text:TextField = new TextField;
			text.wordWrap = false;
			text.multiline = false;
			text.selectable = false;
			
			saveChangeBtn = new Sprite;
			saveChangeBtn.addChild(img);
			saveChangeBtn.addChild(text);
			
			TextFieldUtils.setHtmlText(text,HtmlText.formatSizeColor("<b>保存改变</b>",0x000000,18));
			text.width = text.textWidth + 5;
			text.height = text.textHeight + 5;
			text.x = (img.width - text.width) / 2;
			text.y = (img.height - text.height) / 2;
			
			this.addChild(saveChangeBtn);
			saveChangeBtn.x = _stage.stageWidth * .5;
			saveChangeBtn.y = _stage.stageHeight * ClientConstants.HOME_UP_PER;
			
			saveChangeBtn.buttonMode = true;
			saveChangeBtn.addEventListener(MouseEvent.CLICK,saveHandler);
			
			//========================================
			
			dragContainer = new DragContainerBox(_stage.stageWidth,_stage.stageHeight * .82,_stage);
			this.addChild(dragContainer);
			dragContainer.x = 0;
			dragContainer.y = _stage.stageHeight * ClientConstants.HOME_UP_PER + saveChangeBtn.height;
			
			boxList = new SelfPathEditContainer(_stage,_loader,_lang,_uri);
			boxList.refresh = refresh;
			
			dragContainer.changeData([boxList]);
		}
		
		protected function changeStyleHandler(e:TextEvent):void
		{
			setTimeout(function():void{e.target.setTextFormat(inputFormat);},10);
			
			pathObj.name = titleText.text;
		}
		
		protected function saveHandler(event:MouseEvent):void
		{
			boxList.changePathInfo();
			pathObj.info = boxList.pathData.info;
			pathObj.totalDays = boxList.pathData.info.length;
			
			if(_onSave is Function){
				_onSave(pathObj);
			}
		}
		
		private function refresh():void
		{
			dragContainer.changeData([boxList]);
		}
		
		public function clear():void
		{
			titleText.removeEventListener(TextEvent.TEXT_INPUT,changeStyleHandler);
		}
	}
}